package frame;


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import bean.Driver;
import dao.DriverDaoImpl;

import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;

public class DriverInformationFrame implements ItemListener,ActionListener{

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    //二选一变量提前
    JRadioButton radioButton;
    JRadioButton radioButton_1;
    ButtonGroup bg;

    //
    JComboBox comboBox;
    JComboBox comboBox_1;
    JComboBox comboBox_2;

    JCheckBox chckbxNewCheckBox;
    JCheckBox chckbxC;
    JCheckBox chckbxB;
    JCheckBox chckbxB_1;
    JCheckBox chckbxA;

    JButton button;
    JButton button_1;
    JButton btnNewButton;
    JButton button_2_1;
    JButton button_2;

    String dsex="";
    String dbirth="";
    String dtype="";

    private JTextField textField_4;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DriverInformationFrame window = new DriverInformationFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public DriverInformationFrame() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 590, 500);
        frame.setLocationRelativeTo(null); // 默认居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 572, 453);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel label = new JLabel("\u53F8\u673A\u4FE1\u606F\u5F55\u5165");
        label.setFont(new Font("宋体", Font.PLAIN, 30));
        label.setBounds(193, 13, 195, 42);
        panel.add(label);

        JLabel label_1 = new JLabel("\u53F8\u673A\u59D3\u540D\uFF1A");
        label_1.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1.setBounds(51, 106, 117, 31);
        panel.add(label_1);

        JLabel label_1_1 = new JLabel("\u53F8\u673A\u6027\u522B\uFF1A");
        label_1_1.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_1.setBounds(51, 150, 117, 31);
        panel.add(label_1_1);

        JLabel label_1_2 = new JLabel("\u51FA\u751F\u65E5\u671F\uFF1A");
        label_1_2.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_2.setBounds(51, 194, 117, 31);
        panel.add(label_1_2);

        JLabel label_1_3 = new JLabel("\u5BB6\u5EAD\u5730\u5740\uFF1A");
        label_1_3.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_3.setBounds(51, 238, 117, 31);
        panel.add(label_1_3);

        JLabel label_1_4 = new JLabel("\u51C6\u9A7E\u8F66\u578B\uFF1A");
        label_1_4.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_4.setBounds(51, 282, 117, 31);
        panel.add(label_1_4);

        JLabel label_1_5 = new JLabel("\u7535\u8BDD\u53F7\u7801\uFF1A");
        label_1_5.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_5.setBounds(51, 326, 117, 31);
        panel.add(label_1_5);

        JLabel label_1_6 = new JLabel("\u6709\u6548\u671F\u622A\u6B62\u65F6\u95F4\uFF1A");
        label_1_6.setFont(new Font("宋体", Font.PLAIN, 20));
        label_1_6.setBounds(51, 370, 178, 31);
        panel.add(label_1_6);

        textField = new JTextField();
        textField.setBounds(182, 111, 86, 24);
        panel.add(textField);
        textField.setColumns(10);

        radioButton = new JRadioButton("\u7537");
        radioButton.setBounds(178, 154, 43, 27);
        panel.add(radioButton);

        radioButton_1 = new JRadioButton("\u5973");
        radioButton_1.setBounds(227, 154, 43, 27);
        panel.add(radioButton_1);

        //控制男女选项二选一
        bg=new ButtonGroup();
        bg.add(radioButton);
        bg.add(radioButton_1);


        comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012"}));
        comboBox.setBounds(184, 199, 61, 24);
        panel.add(comboBox);

        comboBox_1 = new JComboBox();
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}));
        comboBox_1.setBounds(254, 199, 43, 24);
        panel.add(comboBox_1);

        comboBox_2 = new JComboBox();
        comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        comboBox_2.setBounds(311, 199, 43, 24);
        panel.add(comboBox_2);

        textField_1 = new JTextField();
        textField_1.setBounds(182, 243, 172, 24);
        panel.add(textField_1);
        textField_1.setColumns(10);

        chckbxNewCheckBox = new JCheckBox("C1");
        chckbxNewCheckBox.setBounds(178, 286, 50, 27);
        panel.add(chckbxNewCheckBox);

        chckbxC = new JCheckBox("C2");
        chckbxC.setBounds(225, 286, 45, 27);
        panel.add(chckbxC);

        chckbxB = new JCheckBox("B1");
        chckbxB.setBounds(272, 286, 50, 27);
        panel.add(chckbxB);

        chckbxB_1 = new JCheckBox("B2");
        chckbxB_1.setBounds(321, 286, 50, 27);
        panel.add(chckbxB_1);

        chckbxA = new JCheckBox("A1");
        chckbxA.setBounds(367, 286, 50, 27);
        panel.add(chckbxA);

        textField_2 = new JTextField();
        textField_2.setBounds(182, 331, 129, 24);
        panel.add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(211, 375, 160, 24);
        panel.add(textField_3);
        textField_3.setColumns(10);

        button = new JButton("\u63D0\u4EA4");
        button.setBounds(55, 414, 113, 27);
        panel.add(button);

        button_1 = new JButton("\u6E05\u9664");
        button_1.setBounds(241, 414, 113, 27);
        panel.add(button_1);

        btnNewButton = new JButton("\u8FD4\u56DE");
        btnNewButton.setBounds(425, 414, 113, 27);
        panel.add(btnNewButton);

        button_2 = new JButton("\u4FEE\u6539");
        button_2.setBounds(445, 95, 113, 27);
        panel.add(button_2);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(346, 111, 86, 24);
        panel.add(textField_4);

        button_2_1 = new JButton("\u5220\u9664");
        button_2_1.setBounds(445, 137, 113, 27);
        panel.add(button_2_1);

        button.addActionListener(this);
        button_1.addActionListener(this);
        button_2.addActionListener(this);
        btnNewButton.addActionListener(this);
        button_2_1.addActionListener(this);

        chckbxNewCheckBox.addItemListener(this);
        chckbxC.addItemListener(this);
        chckbxB.addItemListener(this);
        chckbxB_1.addItemListener(this);
        chckbxA.addItemListener(this);

        radioButton.addItemListener(this);
        radioButton_1.addItemListener(this);

        comboBox.addItemListener(this);
        comboBox_1.addItemListener(this);
        comboBox_2.addItemListener(this);
    }



    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub

        //①男女性别选择
        if(radioButton.isSelected()){
            dsex=radioButton.getText();
        }else if(radioButton_1.isSelected()){
            dsex=radioButton_1.getText();
        }

        //②出生日期选择
        dbirth=comboBox.getSelectedItem().toString()+comboBox_1.getSelectedItem().toString()+comboBox_2.getSelectedItem().toString();


        //③车型选择
        dtype="";
        if(chckbxNewCheckBox.isSelected()){
            dtype+=chckbxNewCheckBox.getText();
        }
        if(chckbxC.isSelected()){
            dtype+=chckbxC.getText();
        }
        if(chckbxB.isSelected()){
            dtype+=chckbxB.getText();
        }
        if(chckbxB_1.isSelected()){
            dtype+=chckbxB_1.getText();
        }
        if(chckbxA.isSelected()){
            dtype+=chckbxA.getText();
        }

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String dname=textField.getText();
        String daddress=textField_1.getText();
        String dnum=textField_2.getText();
        String dusetime=textField_3.getText();
        if(e.getSource()==button){
            DriverDaoImpl dridao=new DriverDaoImpl();
            Driver driver=new Driver();

            driver.setDname(dname);
            driver.setDsex(dsex);
            driver.setDbirth(dbirth);
            driver.setDaddress(daddress);
            driver.setDtype(dtype);
            driver.setDnum(dnum);
            driver.setDusetime(dusetime);

            int i=dridao.addDriver(driver);
            if(i!=0){
                JOptionPane.showMessageDialog(frame, "注册成功！");
                frame.setVisible(false);
            }
        }else if(e.getSource()==button_2){
            DriverDaoImpl dridao=new DriverDaoImpl();
            Driver driver=new Driver();
            driver.setDname(dname);
            driver.setDsex(dsex);
            driver.setDbirth(dbirth);
            driver.setDaddress(daddress);
            driver.setDtype(dtype);
            driver.setDnum(dnum);
            driver.setDusetime(dusetime);
            driver.setDid(Integer.parseInt(textField_4.getText()));
            int i =dridao.updateDriver(driver);
            if(i!=0){
                JOptionPane.showMessageDialog(frame, "修改成功！");
                frame.setVisible(false);
            }
        }else  if(e.getSource()==button_2_1){
            DriverDaoImpl dridao=new DriverDaoImpl();
            Driver driver=new Driver();
            driver.setDid(Integer.parseInt(textField_4.getText()));
            int i =dridao.deleteDriver(driver.getDid());
            if(i!=0){
                JOptionPane.showMessageDialog(frame, "删除成功！");
                frame.setVisible(false);
            }
        }else if(e.getSource()==btnNewButton){
            DriverTableFrame dtf=new DriverTableFrame();
            frame.setVisible(false);
        }else if(e.getSource()==button_1){
            textField.setText("");
            textField_1.setText("");
            textField_2.setText("");
            textField_3.setText("");
            textField_4.setText("");

            bg.clearSelection();

            chckbxNewCheckBox.setSelected(false);
            chckbxC.setSelected(false);
            chckbxB.setSelected(false);
            chckbxB_1.setSelected(false);
            chckbxA.setSelected(false);

            comboBox.setSelectedIndex(0);
            comboBox_1.setSelectedIndex(0);
            comboBox_2.setSelectedIndex(0);
        }
    }
}